<template>
  <div v-loading="loading">
    <el-table :data="tableData" stripe v-on="$listeners">
      <slot></slot>
    </el-table>
    <div style="margin-top: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "CommonTable",
  props: ["getData"],
  mounted() {
    this.getData()
      .then((res) => {
        this.tableData = res.tableData;
        this.total = res.total;
        this.loading = false;
      })
      .catch((e) => {
        console.log(e);
      });
  },
  watch: {
    getData(val) {
      this.loading = true;
      val().then((res) => {
        this.tableData = res.tableData;
        this.total = res.total;
        this.loading = false;
      });
    },
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.$emit("pageChange", { page: this.currentPage, size: val });
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.$emit("pageChange", { page: val, size: this.pageSize });
    },
  },
  data() {
    return {
      currentPage: 1,
      loading: true,
      tableData: [],
      total: 0,
      pageSize: 10,
    };
  },
};
</script>

<style scoped>
</style>
